from django.test import TestCase
import pickle
import base64

# Create your tests here.

# pickle.dumps(dict|对象)：将传入的数据转换为bytes字节流
# pickle.loads(bytes字节流)：将bytes字节流转换为dict|对象

if __name__ == "__main__":
    req_data = 'gAN9cQAoSwF9cQEoWAUAAABjb3VudHECSwJYCAAAAHNlbGVjdGVkcQOIdUsDfXEEKGgCSwFoA4l1dS4='

    # res = req_data.encode()
    # print(res)
    #
    # res = base64.b64decode(res)
    # print(res)
    #
    # res = pickle.loads(res)
    # print(res)

    res = pickle.loads(base64.b64decode(req_data.encode()))
    res = pickle.loads(base64.b64decode(req_data))
    print(res)

if __name__ == "__main__":
    cart_dict = {
        1: {
            'count': 2,
            'selected': True
        },
        3: {
            'count': 1,
            'selected': False
        }
    }

    # # pickle.dumps
    # res = pickle.dumps(cart_dict)
    # print(res)
    #
    # # base64.b64encode
    # res = base64.b64encode(res)
    # print(res)
    #
    # # bytes->str
    # res = res.decode()
    # print(res)

    res = base64.b64encode(pickle.dumps(cart_dict)).decode()
    print(res)